# !usr/bin/env python
# -*-coding:utf-8 -*-

# @FileName: send.py
# @Author:tian
# @Time:06/19/2021

import pika
'''
连接远程服务器
username = 'root'
pwd = '8800'
ip_addr = '10.1.7.7'
port_num = 5672
credentials = pika.PlainCredentials(username, pwd)
connection = pika.BlockingConnection(pika.ConnectionParameters(ip_addr, port_num, '/', credentials))
'''
# 获取连接
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
'''
channel:
<BlockingChannel impl=<Channel number=1 OPEN conn=<SelectConnection OPEN 
transport=<pika.adapters.utils.io_services_utils._AsyncPlaintextTransport object at 0x000001AC41C49550> 
params=<ConnectionParameters host=localhost port=5672 virtual_host=/ ssl=False>>>>

'''
# 从连接中创建通道，使用通道才能完成消息相关的操作
channel = connection.channel()
# durable=True 持久化第一步
channel.queue_declare(queue="hello",durable=True)

# routing_key指定队列名称
channel.basic_publish(exchange='',
                      routing_key='hello',  # 发消息给hello队列
                      body='hello java',
                      properties=pika.BasicProperties(delivery_mode=2)  # 2 = 消息持久化
                      )
print(" [x] Sent 'Hello World!'")
connection.close()